package cn.neusoft.controller;

import cn.neusoft.domain.*;
import cn.neusoft.service.CargoAndDescriptionService;
import cn.neusoft.service.WarehouseAdministrateService;
import cn.neusoft.tool.PageSet;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/warehouseAdmin")
public class WarehouseAdministrateController {

    @Qualifier("warehouseAdministrateImp")
    @Autowired
    private WarehouseAdministrateService warehouseAdminService;

    @Autowired
    private CargoAndDescriptionService cargoAndDescriptionService;
    private static final String UNSETTING = "未设置";

  @PostMapping("/generateWarehouse")
  public synchronized ResultBean generateWarehouseBill(@RequestBody Map<String, Object> map){
    ResultBean resultBean;

    if(map != null){
      try{
        String warehouseName = (String) map.get("warehouseName");
        String address = (String) map.get("address");
        WarehouseLevelEnum level = WarehouseLevelEnum.valueOf((String) map.get("level"));
        String manager = (String) map.get("manager");

        List<Map<String, String>> settinglist = (List<Map<String, String>>) (map.get("cargoSettingInf"));

        Map<String, Double> warnCargosInf = new HashMap<>();
        Map<String, Double> maxCargosInf = new HashMap<>();
        Map<String, Double> cargoInf = new HashMap<>();
        Map<String, Double> preCargos = new HashMap<>();
        Map<String, Double> preOutCargos = new HashMap<>();

        for(Map<String, String> m : settinglist){
            if(!m.get("warnQuantities").equals("")){
                warnCargosInf.put(m.get("cargoName"), Double.valueOf(m.get("warnQuantities")));
            }
            if(!m.get("maxQuantities").equals("")){
                maxCargosInf.put(m.get("cargoName"), Double.valueOf(m.get("maxQuantities")));
            }
            if(!m.get("preQuantities").equals("")){
                preCargos.put(m.get("cargoName"), Double.valueOf(m.get("preQuantities")));
            }
            else{
                preCargos.put(m.get("cargoName"), 0.0);

            }
            if(!m.get("quantities").equals("")){
                cargoInf.put(m.get("cargoName"), Double.valueOf(m.get("quantities")));
            }
            else{
                cargoInf.put(m.get("cargoName"), 0.0);

            }
            if(!m.get("preOutQuantities").equals("")){
                preOutCargos.put(m.get("cargoName"), Double.valueOf(m.get("preOutQuantities")));
            }
            else{
                preOutCargos.put(m.get("cargoName"), 0.0);

            }
        }
        Warehouse warehouse = new Warehouse(warehouseName, address, manager, level, warnCargosInf, maxCargosInf, cargoInf, preCargos, preOutCargos);
        int reply = warehouseAdminService.warehouseRegister(warehouse);
        if(reply > 0){
            resultBean = ResultBean.success();
        }
        else{
            resultBean = ResultBean.error(7024, "未知错误");
        }

      }catch (Exception e){
        e.printStackTrace();
        resultBean = ResultBean.error(7025, "错误");

      }

    }
    else{
      resultBean = ResultBean.error(7020, "输入为空");
    }

    return resultBean;
  }

    @ApiOperation("库房删除")
    @GetMapping("/delete/{id}")
    public synchronized ResultBean warehouseDelete(@PathVariable("id") int id) {
        ResultBean resultBean = null;

        int reply = warehouseAdminService.warehouseDeleteByID(id);
        switch(reply){
            case 1:
                resultBean = ResultBean.success();
                break;
            case 0:
                resultBean = ResultBean.error(6011, "失败");
                break;
            default:
                resultBean = ResultBean.error(6012, "未知错误");
                break;
        }

        return resultBean;
    }

    @ApiOperation("库房信息更新")
    @PostMapping("/update")
    public synchronized ResultBean warehouseChange(@RequestBody Map<String, Object> map) {
      ResultBean resultBean;

      if(map != null){
        try{
          int id = (int) map.get("id");
          Warehouse find = warehouseAdminService.getWarehouseByID(id);
          if(find != null){
              String warehouseName = (String) map.get("warehouseName");
              String address = (String) map.get("address");
              WarehouseLevelEnum level = WarehouseLevelEnum.valueOf((String) map.get("level"));
              String manager = (String) map.get("manager");

              List<Map<String, String>> settinglist = (List<Map<String, String>>) (map.get("cargoSettingInf"));

              Map<String, Double> warnCargosInf = new HashMap<>();
              Map<String, Double> maxCargosInf = new HashMap<>();
              Map<String, Double> cargoInf = new HashMap<>();
              Map<String, Double> preCargos = new HashMap<>();
              Map<String, Double> preOutCargos = new HashMap<>();
              for(Map<String, String> m : settinglist){
                  if(!m.get("warnQuantities").equals("")){
                      warnCargosInf.put(m.get("cargoName"), Double.valueOf(m.get("warnQuantities")));
                  }
                  if(!m.get("maxQuantities").equals("")){
                      maxCargosInf.put(m.get("cargoName"), Double.valueOf(m.get("maxQuantities")));
                  }
                  if(!m.get("preQuantities").equals("")){
                      preCargos.put(m.get("cargoName"), Double.valueOf(m.get("preQuantities")));
                  }
                  else{
                      preCargos.put(m.get("cargoName"), 0.0);

                  }
                  if(!m.get("quantities").equals("")){
                      cargoInf.put(m.get("cargoName"), Double.valueOf(m.get("quantities")));
                  }
                  else{
                      cargoInf.put(m.get("cargoName"), 0.0);

                  }
                  if(!m.get("preOutQuantities").equals("")){
                      preOutCargos.put(m.get("cargoName"), Double.valueOf(m.get("preOutQuantities")));
                  }
                  else{
                      preOutCargos.put(m.get("cargoName"), 0.0);

                  }


              }

              find.setName(warehouseName);
              find.setManager(manager);
              find.setLevel(level);
              find.setCargoWamLevel(warnCargosInf);
              find.setCargoMaxLevel(maxCargosInf);
              find.setPreCargos(preCargos);
              find.setCargos(cargoInf);
              find.setAddress(address);
              find.setPreOutCargos(preOutCargos);
              int reply = warehouseAdminService.warehouseChange(find);
              switch(reply){
                  case 1:
                      resultBean = ResultBean.success();
                      break;
                  case 0:
                      resultBean = ResultBean.error(7031, "失败");
                      break;
                  default:
                      resultBean = ResultBean.error(7034, "未知错误");
                      break;
              }
          }
          else{
              resultBean = ResultBean.error(7034, "未知错误");

          }

        }catch (Exception e){
          e.printStackTrace();
          resultBean = ResultBean.error(7035, "错误");

        }

      }
      else{
        resultBean = ResultBean.error(7030, "输入为空");
      }

      return resultBean;
    }

    @ApiOperation("获取所有库房信息")
    @GetMapping("/getAll")
    public ResultBean warehouseGetAll() {
        ResultBean resultBean = null;

        List<Warehouse> list = warehouseAdminService.warehouseGetAll();
        resultBean = ResultBean.success(list);

        return resultBean;
    }

  @PostMapping("/getAllWarehousePage")
  public ResultBean getAllDepPage(@RequestBody Map<String, String> map){
    ResultBean resultBean;

    List<WarehouseBillReturn> ans = new ArrayList<WarehouseBillReturn>();


    try{
      int pageNum = Integer.parseInt(map.get("pageNum"));
      int pageSize = Integer.parseInt(map.get("pageSize"));
      String name = map.get("warehouseName");
      List<Warehouse> list = warehouseAdminService.warehouseGetAll();


      for(Warehouse c : list){
          if((!name.equals("")) && (!c.getName().equals(name))){
              continue;
          }
        List<WarehouseTableElement> line = new ArrayList<>();
        Map<String, Double> maxMap = c.getCargoMaxLevel();
        Map<String, Double> warnMap = c.getCargoWamLevel();
        Map<String, Double> quanMap = c.getCargos();
        Map<String, Double> preMap = c.getPreCargos();
        Map<String, Double> preOutMap = c.getPreOutCargos();

        for(Map.Entry<String, Double> entry : quanMap.entrySet()){
          CargoDescription cargoDescription;
          cargoDescription = cargoAndDescriptionService.getByDesName(entry.getKey());
          WarehouseTableElement warehouseTableElement;
            String warn = null;
            String max = null;
            if(warnMap.get(entry.getKey()) != null){
                warn = Double.toString(warnMap.get(entry.getKey()));
            }
              else{
                warn = UNSETTING;
            }
            if(maxMap.get(entry.getKey()) != null){
                max = Double.toString(maxMap.get(entry.getKey()));
            }
              else{
                max = UNSETTING;
            }
          if(cargoDescription != null){

            warehouseTableElement = new WarehouseTableElement(entry.getKey(),
              Integer.toString(cargoDescription.getId()), cargoDescription.getUnit(), warn, max,
                    Double.toString(entry.getValue()), Double.toString(preMap.get(entry.getKey())), Double.toString(preOutMap.get(entry.getKey())));
          }
          else{
            warehouseTableElement = new WarehouseTableElement(entry.getKey(),
              "未设置商品", "未设置商品", warn, max,
                    Double.toString(entry.getValue()), Double.toString(preMap.get(entry.getKey())), Double.toString(preOutMap.get(entry.getKey())));
          }
          line.add(warehouseTableElement);
        }

        WarehouseBillReturn warehouseBillReturn = new WarehouseBillReturn(c.getId(), c.getName(), c.getManager(),
          line, c.getLevel().toString(), c.getAddress());
        ans.add(warehouseBillReturn);

      }


      PageSet<WarehouseBillReturn> pageSet = new PageSet<WarehouseBillReturn>(ans, pageSize);
      Map<String, Object> answer = new HashMap<>();
      answer.put("total", pageSet.getTotal());
      answer.put("list", pageSet.getPage(pageNum));

      resultBean = ResultBean.success(answer);

    }catch(Exception e){
      e.printStackTrace();
      resultBean = ResultBean.error(1, "错误");
    }

    return resultBean;
  }

  private class WarehouseBillReturn {
    private int id;
    private String warehouseName;
    private String manager;

    private String address;
    private List<WarehouseTableElement> list;
    private String level;

    public WarehouseBillReturn(int id, String warehouseName, String manager, List<WarehouseTableElement> list, String level, String address) {
      this.id = id;
      this.warehouseName = warehouseName;
      this.manager = manager;
      this.list = list;
      this.level = level;
      this.address = address;
    }

      public String getAddress() {
          return address;
      }

      public void setAddress(String address) {
          this.address = address;
      }

      public int getId() {
      return id;
    }

    public void setId(int id) {
      this.id = id;
    }

    public String getWarehouseName() {
      return warehouseName;
    }

    public void setWarehouseName(String warehouseName) {
      this.warehouseName = warehouseName;
    }

    public String getManager() {
      return manager;
    }

    public void setManager(String manager) {
      this.manager = manager;
    }

    public List<WarehouseTableElement> getList() {
      return list;
    }

    public void setList(List<WarehouseTableElement> list) {
      this.list = list;
    }

    public String getLevel() {
      return level;
    }

    public void setLevel(String level) {
      this.level = level;
    }
  }

  private class WarehouseTableElement {
    private String cargoName;
    private String id;
    private String unit;
    private String warnQuantities;
    private String maxQuantities;
    private String quantities;

    private String preQuantities;

    private String preOutQuantities;

      public String getPreQuantities() {
          return preQuantities;
      }

      public String getPreOutQuantities() {
          return preOutQuantities;
      }

      public void setPreOutQuantities(String preOutQuantities) {
          this.preOutQuantities = preOutQuantities;
      }

      public void setPreQuantities(String preQuantities) {
          this.preQuantities = preQuantities;
      }

      public String getCargoName() {
      return cargoName;
    }

      public String getQuantities() {
          return quantities;
      }

      public void setQuantities(String quantities) {
          this.quantities = quantities;
      }

      public void setCargoName(String cargoName) {
      this.cargoName = cargoName;
    }

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getUnit() {
      return unit;
    }

    public void setUnit(String unit) {
      this.unit = unit;
    }

    public String getWarnQuantities() {
      return warnQuantities;
    }

    public void setWarnQuantities(String warnQuantities) {
      this.warnQuantities = warnQuantities;
    }

    public String getMaxQuantities() {
      return maxQuantities;
    }

    public void setMaxQuantities(String maxQuantities) {
      this.maxQuantities = maxQuantities;
    }

      public WarehouseTableElement(String cargoName, String id, String unit, String warnQuantities, String maxQuantities, String quantities, String preQuantites, String preOutQuantities) {
          this.cargoName = cargoName;
          this.id = id;
          this.unit = unit;
          this.warnQuantities = warnQuantities;
          this.maxQuantities = maxQuantities;
          this.quantities = quantities;
          this.preQuantities = preQuantites;
          this.preOutQuantities = preOutQuantities;
      }
  }

  }
